/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_host_hdi.h"
#include "sle_hal_errcode.h"
#include "sle_unit_test_log.h"
static SleHostHdiCallbacks *g_hostHdiTestCbks = NULL;

static SleDeviceState g_sleDeviceState = SLE_HOST_STATE_ERROR;
SleHdiErrorCode SleHostEnableSleTest(void)
{
    g_sleDeviceState = SLE_HOST_STATE_ENABLED;
    g_hostHdiTestCbks->OnSleEnabledCb(SLE_HDI_ERRCODE_SUCC);
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHostDisableSleTest(void)
{
    g_sleDeviceState = SLE_HOST_STATE_DISABLED;
    g_hostHdiTestCbks->OnSleDisabledCb(SLE_HDI_ERRCODE_SUCC);
    return SLE_HDI_ERRCODE_SUCC;
}

static SleDeviceAddress sleHostAddrPubTest = {
    .addrType = SLE_PUBLIC_ADDRESS_TYPE,
};
static SleDeviceAddress sleHostAddrRandTest = {
    .addrType = SLE_RANDOM_ADDRESS_TYPE,
};
SleHdiErrorCode SleHostSetHostAddressTest(const SleDeviceAddress *addr)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG((addr->addrType == SLE_PUBLIC_ADDRESS_TYPE || addr->addrType == SLE_RANDOM_ADDRESS_TYPE)
        , SLE_HDI_ERRCODE_INVALID_PARAM, "SleHostSetHostAddressTest");
    SLE_UT_CHECK_AND_RETURN_RET_LOG(addr->addr != NULL, SLE_HDI_ERRCODE_INVALID_PARAM, "SleHostSetHostAddressTest");
    if (addr->addrType == SLE_PUBLIC_ADDRESS_TYPE) {
        memcpy(&sleHostAddrPubTest, addr, sizeof(SleDeviceAddress));
        return SLE_HDI_ERRCODE_SUCC;
    } else if (addr->addrType == SLE_RANDOM_ADDRESS_TYPE) {
        memcpy(&sleHostAddrRandTest, addr, sizeof(SleDeviceAddress));
        return SLE_HDI_ERRCODE_SUCC;
    }
    return SLE_HDI_ERRCODE_INVALID_PARAM;
}

SleHdiErrorCode SleHostGetHostAddressTest(SleDeviceAddress *addr)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG((addr->addrType == SLE_PUBLIC_ADDRESS_TYPE || addr->addrType == SLE_RANDOM_ADDRESS_TYPE)
        , SLE_HDI_ERRCODE_FAIL, "SleHostGetHostAddressTest1");
    SLE_UT_CHECK_AND_RETURN_RET_LOG(addr->addr != NULL, SLE_HDI_ERRCODE_INVALID_PARAM, "SleHostGetHostAddressTest2");
    if (addr->addrType == SLE_PUBLIC_ADDRESS_TYPE) {
        memcpy(addr, &sleHostAddrPubTest, sizeof(SleDeviceAddress));
        return SLE_HDI_ERRCODE_SUCC;
    } else if (addr->addrType == SLE_RANDOM_ADDRESS_TYPE) {
        memcpy(addr, &sleHostAddrRandTest, sizeof(SleDeviceAddress));
        return SLE_HDI_ERRCODE_SUCC;
    }
    return SLE_HDI_ERRCODE_INVALID_PARAM;
}

static uint8_t hostNameTest[SLE_DEVICE_NAME_LEN] = {'h','o','s','t','_','t','e','s','t','\0'};
SleHdiErrorCode SleHostSetHostNameTest(const uint8_t *name, uint8_t len)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(name != NULL, SLE_HDI_ERRCODE_INVALID_PARAM, "SleHostSetHostNameTest1");
    SLE_UT_CHECK_AND_RETURN_RET_LOG((len > 0 && len <= SLE_DEVICE_NAME_LEN), SLE_HDI_ERRCODE_FAIL, "SleHostSetHostNameTest2");
    SLE_UT_CHECK_AND_RETURN_RET_LOG((name[len-1] == '\0' || name[len-1] == 0), SLE_HDI_ERRCODE_FAIL, "SleHostSetHostNameTest3");
    memcpy(&hostNameTest, name, len);
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHostGetHostNameTest(uint8_t *name, uint8_t *len)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(name != NULL, SLE_HDI_ERRCODE_INVALID_PARAM, "SleHostGetHostNameTest1");
    SLE_UT_CHECK_AND_RETURN_RET_LOG((*len > 0 && *len <= SLE_DEVICE_NAME_LEN), SLE_HDI_ERRCODE_INVALID_PARAM, "SleHostGetHostNameTest2");
    memcpy(name, &hostNameTest, *len);
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode SleHostRegisterHostTestCbks(SleHostHdiCallbacks *cbks)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(cbks != NULL, SLE_HDI_ERRCODE_INVALID_PARAM, "SleHostRegisterHostTestCbks");
    g_hostHdiTestCbks = cbks;
    return SLE_HDI_ERRCODE_SUCC;
}

SleHdiErrorCode GetSleHostHdiOpts(SleHostHdiOpts *hostOpts)
{
    SLE_UT_CHECK_AND_RETURN_RET_LOG(hostOpts != NULL, SLE_HDI_ERRCODE_INVALID_PARAM, "GetSleHostHdiOpts");
    hostOpts->enableSle = SleHostEnableSleTest;
    hostOpts->disableSle = SleHostDisableSleTest;
    hostOpts->setHostAddress = SleHostSetHostAddressTest;
    hostOpts->getHostAddress = SleHostGetHostAddressTest;
    hostOpts->setHostName = SleHostSetHostNameTest;
    hostOpts->getHostName = SleHostGetHostNameTest;
    hostOpts->registerHostCbks = SleHostRegisterHostTestCbks;
    return SLE_HDI_ERRCODE_SUCC;
}